// pages/getUserDetail/getUserDetail.js
var app = getApp();
var address = require('../../utils/city.js')
var conmon = require('../../utils/conmon.js')
var animation
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modelHidden: true,
    areaInfo:"请选择",
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    queryId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var money;
    if (options.id==1){
      money=5000;
    } else if (options.id == 2) {
      money = 10000;
    } else if (options.id == 3) {
      money = 30000;
    } else if (options.id == 4) {
      money = 50000;
    }
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          userName: options.name,
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          merchantType:  options.id,
          money:money
        });
      }
    });
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

  , addressInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        addressInput: e.detail.value
      })
    }
  }
  , areaInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        areaInput: e.detail.value
      })
    }
  }
  , priceInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        priceInput: e.detail.value
      })
    }
  }
  , realInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        realInput: e.detail.value
      })
    }
  }
  , timeInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        timeInput: e.detail.value
      })
    }
  }
  , phoneInput: function (e) {
    if (e.detail.value != '') {
      this.setData({
        phoneInput: e.detail.value
      })
    }
  },
  recommendPhone:function(e){
    if (e.detail.value != '') {
      this.setData({
        recommendPhone: e.detail.value
      })
    }else{
      this.setData({
        recommendPhone: ''
      })
    }
    
  }
  , sendMsgToBack: function () {
    var that = this;
    var reg = /^1[34578]\d{9}$/
  //  console.log(that.data.phoneInput);
    if (that.data.phoneInput && that.data.addressInput && that.data.realInput && that.data.areaInput  && that.data.priceInput) {    
      if (!reg.test(that.data.phoneInput)) {
        wx.showToast({
          title: "请输入正确的手机号",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
      if (that.data.recommendPhone){
        if (!reg.test(that.data.recommendPhone)) {
          wx.showToast({
            title: "请输入正确的推荐人手机号",
            icon: 'loading',
            duration: 2000
          })
          return;
        }
      }
      if (conmon.IdentityCodeValid(that.data.priceInput)!=true){
        wx.showToast({
          title: "请输入正确的身份证号",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
      if (that.data.areaInfo=="请选择"){
        wx.showToast({
          title: "请选择所在地区",
          icon: 'loading',
          duration: 2000
        })
        return;
      }
    } else {
      wx.showToast({
        title: '请填写完整信息!',
        icon:"loading"
      })
      return;
    }
    wx.request({
      url: app.globalData.allUrl + "/tLPayAction/PayAgent",
      data: {
        phone: that.data.phoneInput,
        address: that.data.realInput,
        name: that.data.addressInput,
        identityCard: that.data.priceInput,
        principal: that.data.areaInput,
        agentRegion: that.data.queryId,
        regionName: that.data.areaInfo,
        isMerchant: that.data.merchantType,
        agentId: wx.getStorageSync("userid"),
        userId: wx.getStorageSync("userid"),
        referrerPhone: that.data.recommendPhone,
        merchantType: that.data.merchantType, 
        money: that.data.money,
      //  money: 0.01
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(res);
        if(res.data!=250&&res.data!=false){
          var orderformid2=res.data.reqsn;
          var ss = JSON.parse(res.data.payinfo);
          console.log("后台支付回调")
          console.log(ss)
          wx.requestPayment({//如果从后台获取支付信息成功。则调用微信支付接口
            timeStamp: ss.timeStamp,
            nonceStr: ss.nonceStr,
            package: ss.package,
            signType: "MD5",
            paySign: ss.paySign,
            success: function (res) {
              console.log(res)
              wx.showToast({
                title: '微信支付成功',
                icon: 'success',
                duration: 2000
              })
              wx.request({
                url: app.globalData.allUrl + "/userAction/getUserInfoById",
                data:{
                  UUID:wx.getStorageSync("userid")
                },
                success:function(res){
                  console.log(res)
                  var userTypeName;
                  if (res.data.isMerchant == 1) {
                    userTypeName = "体验庄主"
                  } else if (res.data.isMerchant == 2) {
                    userTypeName = "禾怡庄主"
                  } else if (res.data.isMerchant == 3) {
                    userTypeName = "古堡庄主"
                  } else if (res.data.isMerchant == 4) {
                    userTypeName = "金钥匙庄主"
                  } else if (res.data.isMerchant == 5) {
                    userTypeName = "管理员庄主"
                  }
                  wx.setStorageSync('userTypeName', userTypeName)
                  wx.setStorageSync("userType", res.data.isMerchant)
                  wx.setStorageSync("isUpdatePrice", 1);
                  wx.setStorageSync("userBalance", res.data.balance);   
                  wx.setStorageSync("cartLists", "");
                  wx.setStorageSync("agentId", res.data.agentId);                                                 
                },fail:function(err){
                  console.log(err)
                }
              })
            }, fail(res) {
              wx.showModal({
                title: '提示',
                content: '支付失败！',
              })
              wx.request({
                url: app.globalData.allUrl + "/tLPayAction/payDefeated",
                data: {
                  orderFormId: orderformid2
                },
                success: function (res) {
                }})
             }
          })
        }else if(res.data==250){
          wx.showModal({
            title: '提示',
            content: '支付成功！商家正在审核中，请勿重复提交！',
          })
        } else if (res.data == false) {
          wx.showModal({
            title: '提示',
            content: '检查到您已成为庄主，如需充值请前往我的-余额充值界面进行充值',
          })
        }
      }
    })

  },
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name 
    that.setData({
      areaInfo: areaInfo,
      queryId: that.data.citys[value[2]].id
    })
  },
  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
  },
  // 选择状态按钮
  selectState: function (e) {
    console.log('selectState1')
    this.startAnimation(false, -200)
    var status = e.currentTarget.dataset.status
    this.setData({
      status: status
    })
    console.log(this.data)

  },
  // 日志选择
  bindDateChange: function (e) {
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        begin: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        end: e.detail.value
      })
    }
  },
  sureDateTap: function () {
    this.data.pageNo = 1
    this.startAnimation(false, -200)
  },
})